﻿using Microsoft.AspNetCore.Components;
using SE.PO.CInterface.CuCoding.Signals;
using SE.PO.CInterface.DBProtocols.Areas;
using SE.PO.CInterface.DBProtocols.Sites;
using SE.PO.CInterface.Web.Models;

namespace SE.PO.CInterface.Web.Pages.DB.Components;

public partial class SelectDeviceType
{
    private List<SelectModelItem> _deviceTypes = new();


    private List<SignalCodingDto>? _signalCodingDtos = new();

    [Parameter] public string? DeviceType { get; set; }

    [Parameter] public dynamic? Model { get; set; }

    [Parameter] public EventCallback<string> SelectDeviceTypeChanged { get; set; }

    protected override async Task OnInitializedAsync()
    {
        _signalCodingDtos = (await SignalsCodingClient.Get()!)?.ToList();
        var list = (from s in _signalCodingDtos
                select new SelectModelItem(s.DeviceType, s.DeviceCode)
            ).ToList();
        _deviceTypes = list.GroupBy(d => new {d.Label, d.Value}).Select(d => d.First()).ToList();
    }

    private async Task OnSelectedItemChangedHandler(SelectModelItem item)
    {
        if (Model != null)
        {
            Model.DeviceType = item.Value;
        }

        await SelectDeviceTypeChanged.InvokeAsync(item.Value);
    }
}